package tw.fc.agui;

import android.content.Context;
import android.support.v4.view.MotionEventCompat;
import android.util.Log;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.GestureDetector;
import android.view.View;
import android.view.MotionEvent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Bitmap;
import android.graphics.Paint;

public abstract class GrPanel extends View 
                              implements GestureDetector.OnGestureListener,
                                         GestureDetector.OnDoubleTapListener{
    
   private final int   DEFAULT_BG_COLOR= Color.argb(100, 30, 255, 30); //草綠色  
   //[ 東西將繪製在此張 bitmap上
   private Bitmap NEW_BITMAP;
   //[ grCanvas模擬 java.awt.Graphics的方法, 供程式師繪圖
   private final GrCanvas GRCANVAS= new GrCanvas();
   
   private int bgColor= DEFAULT_BG_COLOR;
   //[ 負責發配各種觸碰事件
   private GestureDetector mDetector;   
   //[ 繪圖時經常會用到
   private int screenWidth, screenHeight;
   // ---------- construct ----------
   public GrPanel(Context context) { 
      super(context);
      gestureDetector(context);
      //[ 取得設備的寬與高
      DisplayMetrics metrics = context.getResources().getDisplayMetrics();
      setBitmapIntoCanvas(metrics.widthPixels, metrics.heightPixels);
   }
   public GrPanel(Context context, int w, int h) { 
      super(context);
      gestureDetector(context);
      setBitmapIntoCanvas(w,h);
   }
   public GrPanel(Context context, AttributeSet attrs, int w, int h) { 
      super(context, attrs); 
      gestureDetector(context);
      setBitmapIntoCanvas(w,h);
   }
   public GrPanel(Context context, AttributeSet attrs, int defStyle, int w, int h) { 
      super(context, attrs, defStyle); 
      gestureDetector(context);
      setBitmapIntoCanvas(w,h);
   }
   // ---------- construct ----------
   private final void gestureDetector(Context context) {
      mDetector = new GestureDetector(context, this);
      mDetector.setOnDoubleTapListener(this);
   }    
   
   private final void setBitmapIntoCanvas(int width, int height) {      
      NEW_BITMAP=Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
      GRCANVAS.setBitmap(NEW_BITMAP);
      screenWidth= width; screenHeight= height;
   }
   
   public final void setBackground(int color) { bgColor= color; }  
   public final int getScreenWidth()  { return screenWidth;  }
   public final int getScreenHeight() { return screenHeight; }
   
   //[ 供覆寫的反應函數, 若回傳 true表示觸碰行為往下繼續處理.   
   @Override 
   public boolean onFling(MotionEvent e1, MotionEvent e2, 
                          float velocityX, float velocityY){ 
      return true;
   }
   @Override 
   public boolean onScroll(MotionEvent e1, MotionEvent e2, 
                           float distanceX, float distanceY){ 
      return true;
   }
   @Override public boolean onDown(MotionEvent e)               { return true; }
   @Override public boolean onDoubleTap(MotionEvent e)          { return true; }
   @Override public boolean onDoubleTapEvent(MotionEvent e)     { return true; }
   @Override public boolean onSingleTapConfirmed(MotionEvent e) { return true; }
   @Override public boolean onSingleTapUp(MotionEvent e)        { return true; }
   @Override public void    onLongPress(MotionEvent e)          {}
   @Override public void    onShowPress(MotionEvent e)          {}
             public boolean    onMove(MotionEvent e)            { return true; }
             public boolean    onRelease(MotionEvent e)         { return true; }
             public boolean onMultiTouchDown(MotionEvent e)     { return true; }
             public boolean onMultiTouchUp(MotionEvent e)       { return true; }
   //]
          
   @Override
   public final boolean onTouchEvent(MotionEvent e) {
      boolean returnValue= true;   
      //[ ACTION_DOWN能被 onDown取代, 其他行為仍有存在的必要. 
      switch(MotionEventCompat.getActionMasked(e)) {
         case (MotionEvent.ACTION_MOVE) :
            returnValue= onMove(e);
            break;
         case (MotionEvent.ACTION_UP) :
            returnValue= onRelease(e);
            break;
         case (MotionEvent.ACTION_POINTER_DOWN) :
            returnValue= onMultiTouchDown(e);
            break;
         case (MotionEvent.ACTION_POINTER_UP) :
            returnValue= onMultiTouchUp(e);
            break;
      }
      if(returnValue) {
         returnValue= mDetector.onTouchEvent(e);
      }          
      return returnValue;  
   }
   
   //[ GrCanvas繼承 Canvas, 並擴充繪製點, 線, 面的方法,
   //[ 所擴充的方法使用方式與 java.awt.Graphics相同.
   //[
   //[ 因此, 當繼承 GrPanel時應覆寫 onDraw(GrCanvas grCanvas), 
   //[ 而非 onDraw(Canvas c).
   public void onDraw(GrCanvas grCanvas) {}
   @Override
   public void onDraw(Canvas c) {
      super.onDraw(c);
      GRCANVAS.drawColor(Color.WHITE);
      GRCANVAS.drawColor(bgColor);
      onDraw(GRCANVAS);
      c.drawBitmap(NEW_BITMAP, 0, 0, new Paint());
   }
}
